function deepMerge(obj1, obj2) {
    if (obj1 === undefined) {
        return obj2;
    }
    Object.keys(obj2).forEach(key => {
        if (obj1[key] == undefined) {
            obj1[key] = obj2[key];
        } else if (Array.isArray(obj2[key])) {
            obj1[key] = deepMerge(obj1[key], obj2[key])
        } else if (typeof obj2[key] == 'object' && obj2[key]) {
            obj1[key] = deepMerge(obj1[key] || {}, obj2[key]);
        } else {
            obj1[key] = obj2[key];
        }
    })
    return obj1;
}

console.log(deepMerge({
    id: 1,
    children: [{
        id: 'child1'
    }, {
        id: 'child2'
    }]
}, {
    id: 2,
    name: 2,
    children: [{
        name: 'child22'
    }]
}))